"""剪切板项目组件模块"""

from PySide6.QtWidgets import QWidget, QHBoxLayout, QLabel
from PySide6.QtCore import Qt, QSize
from PySide6.QtGui import QFont, QFontMetrics

from .database import ClipboardItem
from .clipboard_monitor import ClipboardUtils


class ClipboardItemWidget(QWidget):
    """剪切板项目widget - 简洁单行版本"""
    
    def __init__(self, item: ClipboardItem, parent=None):
        super().__init__(parent)
        self.item = item
        self.setup_ui()
    
    def setup_ui(self):
        """设置UI - 单行紧凑布局，所有子组件垂直居中"""
        # 设置紧凑高度和最小宽度
        self.setMinimumWidth(300)
        
        # 主布局 - 单行水平布局
        main_layout = QHBoxLayout(self)
        main_layout.setContentsMargins(8, 0, 8, 0)
        main_layout.setSpacing(8)
        main_layout.setAlignment(Qt.AlignVCenter)  # 保证所有子组件垂直居中
        
        # 时间标签
        time_label = QLabel(self.item.timestamp.strftime("%H:%M:%S"))
        time_label.setStyleSheet("""
            color: #6c757d;
            font-size: 10px;
            font-weight: 500;
            background-color: #e9ecef;
            padding: 2px 6px;
            border-radius: 3px;
        """)
        time_label.setFixedHeight(20)
        time_label.setAlignment(Qt.AlignCenter | Qt.AlignVCenter)
        main_layout.addWidget(time_label, 0, alignment=Qt.AlignVCenter)
        
        # 类型标签
        type_label = QLabel(self.get_type_display_name())
        type_label.setStyleSheet("""
            color: #495057;
            font-size: 9px;
            font-weight: 600;
            background-color: #dee2e6;
            padding: 2px 6px;
            border-radius: 3px;
        """)
        type_label.setFixedHeight(20)
        type_label.setAlignment(Qt.AlignCenter | Qt.AlignVCenter)
        main_layout.addWidget(type_label, 0, alignment=Qt.AlignVCenter)
        
        # 内容预览 - 占据剩余空间
        preview_text = ClipboardUtils.get_content_preview(
            self.item.content, 
            self.item.content_type, 
            200
        )
        content_label = QLabel(preview_text)
        content_label.setWordWrap(False)
        content_label.setAlignment(Qt.AlignLeft | Qt.AlignVCenter)
        
        font = QFont("Segoe UI", 9)
        content_label.setFont(font)
        content_label.setStyleSheet("""
            color: #212529;
            padding: 0;
        """)
        
        content_label.setMinimumWidth(100)
        main_layout.addWidget(content_label, 1, alignment=Qt.AlignVCenter)  # 拉伸因子为1，垂直居中
        
        # 大小标签
        size_label = QLabel(ClipboardUtils.format_size(self.item.size))
        size_label.setStyleSheet("""
            color: #6c757d;
            font-size: 10px;
            font-weight: 500;
        """)
        size_label.setFixedHeight(20)
        size_label.setAlignment(Qt.AlignCenter | Qt.AlignVCenter)
        main_layout.addWidget(size_label, 0, alignment=Qt.AlignVCenter)
        
        # 设置整体样式
        self.setStyleSheet("""
            ClipboardItemWidget {
                background-color: transparent;
                border: none;
                border-radius: 4px;
                margin: 1px;
            }
            ClipboardItemWidget:hover {
                background-color: transparent;
                border: none;
            }
            ClipboardItemWidget:selected {
                background-color: #e3f2fd;
                border: none;
            }
        """)
        
    def get_type_display_name(self) -> str:
        """获取类型显示名称"""
        type_names = {
            "text": "文本",
            "image": "图片", 
            "files": "文件",
            "html": "网页",
            "url": "链接"
        }
        return type_names.get(self.item.content_type, self.item.content_type.upper())
    
    def sizeHint(self) -> QSize:
        """返回建议尺寸"""
        return QSize(400, 32)
    
    def minimumSizeHint(self) -> QSize:
        """返回最小尺寸"""
        return QSize(300, 32) 